<template>
	<view class="follow">
		<!-- 导航栏 -->
		<view class="follow-tab">
			<view class="follow-tab-box">
				<view class="follow-tab-btn" :class="{active:activeIndex===0}" @click="clickTab(0)">文章</view>
				<view class="follow-tab-btn" :class="{active:activeIndex===1}" @click="clickTab(1)">作者</view>
			</view>
		</view>
		<!-- 内容 -->
		<view class="follow-list">
			<swiper class="follow-list-swiper" :current='activeIndex' @change='change'>
				<!-- 文章 -->
				<swiper-item>
					<scroll-list>
						<uni-load-more v-if="articles.length === 0 && !noData" iconType="snow" status="loading"></uni-load-more>
						<card-list types='follow' v-for="(item,index) in articles" :key="index" :item="item"></card-list>
						<view class="no-data" v-show='noData'>没有数据</view>
					</scroll-list>
				</swiper-item>
				<!-- 作者 -->
				<swiper-item>
					<scroll-list>
						<author-list v-for="(item,index) in articles" :key='index' :item='item'></author-list>
					</scroll-list>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				activeIndex:0,
				articles:[],
				noData: false
			}
		},
		onLoad() {
			// 自定义事件$on只能在打开的页面触发
			uni.$on('update_article',(e)=>{
				this.getFollow()
			})
			uni.$on('update_author',()=>{
				this.getAuthors()
			})
			this.getFollow()
			this.getAuthors()
		},
		methods: {
			getAuthors(){
				this.$api.getAuthors().then(res=>{
					const {data} = res
					this.articles = data
				})
			},
			// swiper切换的时候回触发@change事件
			change(e){
				this.activeIndex = e.detail.current
			},
			clickTab(index){
				this.activeIndex = index
			},
			getFollow(){
				this.$api.getFollow().then(res=>{
					const {data} = res
					this.articles = data
					if(this.articles.length === 0){
						this.noData = true
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		height: 100%;
		display: flex;
	}
	.follow{
		height: 100%;
		display: flex;
		flex-direction: column;
		flex: 1;
		box-sizing: border-box;
		.follow-tab{
			height: 30px;
			padding: 10px 20px;
			border-bottom: 1px solid #F5F5F5;
			.follow-tab-box{
				width: 100%;
				height: 100%;
				display: flex;
				border-radius: 5px;
				border: 1px solid $mk-base-color;
				.follow-tab-btn{
					display: flex;
					justify-content: center;
					align-items: center;
					width: 100%;
					font-size: 14px;
					color: #666;
					&:first-child{
						border-right: 1px solid $mk-base-color;
					}
					&.active{
						color: $mk-base-color;
					}
				}
			}
		}
		.follow-list{
			flex: 1;
			.follow-list-swiper{
				height: 100%;
				.swiper-item{
					height: 100%;
				}
			}
		}
	}
	.no-data{
		padding: 50px;
		font-size: 14px;
		color: #999;
		text-align: center;
	}
</style>
